<?php
namespace app\seller\controller;
use app\common\controller\Seller;
use app\common\model\Shop as ShopModel;
use app\common\model\ShopSort;
use app\common\model\ShopShip as shipModel;
use app\common\model\User;
use think\Db;
class Shop extends Seller
{

    /**
     * 添加发货地址
     * @return array|mixed
     */
    public function add(){
        $shipModel = new ShipModel();
        if ($this->request->isPost()) {
            $params=$this->request->param();
            $params['shop_id']=$this->shopinfo->id;
            list($res,$msg)=$shipModel->updateShip($params);
            if(!$res){
                return json(['code'=>0,'msg'=>$msg]);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        return $this->fetch('add');
    }

    /**
     * 编辑发货地址
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id=$this->request->param('id/d');
        $shipModel = new ShipModel();
        if($this->request->isPost()) {
            $params=$this->request->param();
            list($res,$msg)=$shipModel->updateShip($params);
            if(!$res){
                return json(['code'=>0,'msg'=>$msg]);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data   = $shipModel->get($id);
        $this->assign('data', $data);
        return $this->fetch('edit');
    }
    /**
     * 发货地址
     */
    public function address(){
        if($this->request->isAjax()){
            $shiModel = new ShipModel();
            $post=input('param.');
            $post['shop_id']=$this->shopinfo->id;
            return $shiModel->tableData($post);
        }
        return view('list');
    }
    /**
     * 店铺资料修改
     * @return \think\response\View
     */
    public function setting(){
        if(request()->isPost()){
            $data=$this->request->param('row/a');
            $result=$this->shop->where(['id'=>$this->shopinfo->id])->update($data);
            if(!$result){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $sort=new ShopSort();
        $sortlist=$sort->getList();
        $this->assign('sortlist',$sortlist);
        return view();
    }

	/**
	 * 店铺审核
	 * @return [type] [description]
	 */
	public function checkshop()
	{
		if(request()->isPost()){
            Db::startTrans();
            $data=input('post.');
            $info=(new ShopModel)->where('id',$data['id'])->where('status',0)->find();
            if(!$info){
                Db::rollback();
                return json(['code'=>0,'msg'=>'数据错误']);
            }
            $info->status=$data['status'];
            $info->utime=time();
            if(!$info->save()){
                Db::rollback();
                return json(['code'=>0,'msg'=>'审核失败']);
            }
            if($data['status']==1){
                $user=(new User)->where('id',$info->uid)->where('is_shop',0)->find();
                if(!$user){
                    Db::rollback();
                    return json(['code'=>0,'msg'=>'用户信息错误']);
                }
                $user->is_shop=1;
                if(!$user->save()){
                    Db::rollback();
                    return json(['code'=>0,'msg'=>'审核失败']);
                }
            }
            Db::commit();
            return json(['code'=>1,'msg'=>'成功']);
        }
	}
	/**
	 * 类目列表
	 * @return [type] [description]
	 */
	public function sortlist()
	{
		if(request()->isPost()){
			$data=input('post.');
            $this->searchvar($data);
            $list=(new ShopSort)->withSearch(
                ['ctime','name','status'],
                ['ctime'=>[$this->start,$this->end],'name'=>$this->search,'status'=>$this->status])
            ->order('id asc')
            ->select();
            return json(['code'=>0,'data'=>category($list),'count'=>0]);
		}else{
			return view();
		}
	}
	/**
	 * 保存类目
	 * @return [type] [description]
	 */
	public function addsort()
	{
		$id=input('get.id');
		if(request()->isPost()){
			$data=input('post.');
			if(!isset($data['status'])){
				$data['status']=0;
			}
			if($id){
				$i=(new ShopSort)->where('id',$id)->update($data);
			}else{
				$i=(new ShopSort)->save($data);
			}			
			if(!$i){
				return json(['code'=>0,'msg'=>'保存失败']);
			}
			return json(['code'=>1,'msg'=>'保存成功']);
		}else{
			$list=(new ShopSort)->where('pid',0)->where('status',1)->select();
			$assign['list']=$list;
			if($id){
				$info=(new ShopSort)->where('id',$id)->find();
				$assign['info']=$info;
			}
			return view('',$assign);
		}
	}
	/**
	 * 删除类目
	 * @return [type] [description]
	 */
	public function delsort()
	{
		if(request()->isPost()){
			$id=input('post.id');
			$i=(new ShopSort)->where('id',$id)->delete();
			if(!$i){
				return json(['code'=>0,'msg'=>'删除失败']);
			}
			return json(['code'=>1,'msg'=>'删除成功']);
		}
	}
}